package ui.loading
{
	import core.Global;
	
	import events.Msg_FileDownload;
	import events.Msg_FileDownloading;
	import events.Msg_LoadingInit;
	
	import flash.display.Sprite;
	
	import interfaces.ILoading;
	import interfaces.IMsgListener;

	public class LoadingContainer extends Sprite implements IMsgListener
	{
		private static var MSG_TABLE:Array = [
				Msg_FileDownloading.M_FILEDOWNLOADING,
				Msg_FileDownload.M_FILEDOWNLOAD,
			];
		
		private var m_loading:ILoading;
		
		public function LoadingContainer()
		{
			Global.addListeners(MSG_TABLE,this);
			m_loading = new McLoading(this);
			addChild(m_loading.target);
		}
		
		public function msgManager(e:* = null):void
		{
			switch( e.data.msgid)
			{
				case Msg_FileDownloading.M_FILEDOWNLOADING:
					onFileLoading(e.data);
					break;
				case Msg_FileDownload.M_FILEDOWNLOAD:
					onFileLoaded(e.data);
					break;
			}
		}
		
		public function sendInitMsg():void
		{
			Global.dispatchEvent(Msg_LoadingInit.M_LOADINIT,Msg_LoadingInit.createMessage());
		}
		
		private function onFileLoading(e:Msg_FileDownloading):void
		{
			//trace(e.url," ",e.percent," loading");
			m_loading.showLoading(e.percent);
		}
		
		private function onFileLoaded(e:Msg_FileDownload):void
		{
			//trace(e.url," loaded...");
			m_loading.hide();
			
		}
	}
}